package com.example.twitter;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import twitter4j.TwitterException;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import android.widget.ImageView;
import android.widget.TableLayout;

import android.widget.TextView;

public class Perfil extends Activity {
	
		User usuario;
		ConfiguracionTwitter config;
		TextView textUser;
		TextView textScreenName;
		ImageView imagenPerfil;
		TextView columnaFollowersNumero;
		

		@Override
		protected void onCreate(Bundle savedInstanceState){
			super.onCreate(savedInstanceState);
			
			setContentView(R.layout.perfil);
			
			textUser = (TextView) this.findViewById(R.id.textViewUserPerfil);
			textScreenName = (TextView) this.findViewById(R.id.textViewScreenNamePerfil);
			imagenPerfil = (ImageView) this.findViewById(R.id.imagePerfilUser);
			
			columnaFollowersNumero = (TextView) this.findViewById(R.id.ColumnaFollowersNumero);
		
			
			config = new ConfiguracionTwitter(this);
			SharedPreferences prefs = this.getSharedPreferences("MySettingsTwitter", 0);
			config.token = prefs.getString("token", "");
			config.tokenSecret = prefs.getString("tokenSecret", "");
			config.accessToken = new AccessToken(config.token, config.tokenSecret);
			
			config.twitter.setOAuthAccessToken(config.accessToken);
			
			Bundle bundle = this.getIntent().getExtras();
			
			try {
				usuario = config.twitter.showUser(bundle.getLong("id"));
			} catch (TwitterException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			CargarDatos();
			
		}
		
		private void CargarDatos(){
			
			textUser.setText(usuario.getName());
			textScreenName.setText("@" + usuario.getScreenName());
			
			URL url = null;
			
			try {
				url = new URL(usuario.getProfileImageURL());
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			InputStream input = null;
			try {
				input = (InputStream) url.getContent();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Drawable d = Drawable.createFromStream(input, null);
			
			this.imagenPerfil.setImageDrawable(d);
						
			columnaFollowersNumero.setText(String.valueOf(usuario.getFollowersCount()));
					
			
			
			
			
			
			
		}
}
